﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAO_DTO_WS.DTO;
using System.Data.SqlClient;

namespace DAO_DTO_WS.DAO
{
    public class NhanTinKhuyenMaiDAO : Abstract
    {
        public static bool ThemEmail(NhanTinKhuyenMaiDTO temp)
        {
            SqlConnection connection = KetNoi();
            if (KiemTraEmail(temp.Email))
                return false;
            bool KetQua = false;
            String SqlString = "INSERT INTO NHANTINKHUYENMAI(Email,MaDiaPhuong) VALUES " +
                 "(@Email,@DiaPhuong)";
            try
            {
                SqlCommand command = new SqlCommand(SqlString, connection);
                command.Parameters.AddWithValue("@Email", temp.Email);
                command.Parameters.AddWithValue("@DiaPhuong", temp.MaDiaPhuong);
                int rows = command.ExecuteNonQuery();
                if (rows == 1) KetQua = true;
            }
            catch
            {
                KetQua = false;
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return KetQua;
        }
        public static bool KiemTraEmail(string email)
        {
            
            SqlConnection connection = KetNoi();
            ThanhVienDTO tv = new ThanhVienDTO();
            bool kt = false;
            String SqlString = "select * from NHANTINKHUYENMAI where Email=@Email";
            try
            {
                SqlCommand lenh = new SqlCommand(SqlString, connection);
                lenh.Parameters.AddWithValue("@Email", email);
                SqlDataReader Doc = lenh.ExecuteReader();
                kt = Doc.Read();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return kt;
        }
    }
}
